#include <BezierInterpolationMap.h>
#include <BezierColorMap.h>
#include <Color.h>
#include <ColorConvert.h>
#include <ValuePoint.h>
#include <ColorConvert.h>
#include <Average.h>

#define REDPIN 5
#define GREENPIN 3
#define BLUEPIN 6

boolean logging = false;
BezierColorMap* colorMap;
int maxValue;


void setup() {
  Serial.begin(9600);
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  createBezierColorMap();
  debugln("initialized");    
}

void loop() {
  for (int i = 0; i < 1023; i++) {
    RGB color = colorMap->getColorForValue(i);    
    debug(i);
    debug("/");
    debug(colorMap->redMap->maxInputValue);
    float u = colorMap->redMap->getStep(i);
    debug(" = ");
    debug(u);
    debug(", ");
    debug(color.r);
    debug("\n");
    sendColorValue(color);
    delay(2);
    
    
  }
}

void sendColorValue(RGB color) {
    sendColorToPin(REDPIN, color.r);
    sendColorToPin(GREENPIN, color.g);
    sendColorToPin(BLUEPIN, color.b);
}

void sendColorToPin(int pin, float color) {
//  int value = 255 * color;
  color = constrain(color, 0, 255);
  analogWrite(pin, color);
}

void createBezierColorMap() {
  colorMap = newBezierColorMap();
  colorMap->redMap->setControlPoint1(254, 216);
  colorMap->redMap->setControlPoint2(583, 272);
  colorMap->greenMap->setControlPoint1(400, 201);
  colorMap->greenMap->setControlPoint2(1003, 30);
  colorMap->blueMap->setControlPoint1(1093, 1);
  colorMap->blueMap->setControlPoint2(1003, 30);
}

void debug(char* hi) {
  if (logging) {
    Serial.print(hi);
  }
}
void debugln(char* hi) {
  if (logging) {
    Serial.print(hi);
  }
}
void debug(float hi) {
  if (logging) {
    Serial.print(hi);
  }
}

void debug(int hi) {
  if (logging) {
    Serial.print(hi);
  }
}
void debugln(int hi) {
  if (logging) {
    Serial.print(hi);
  }
}
